package com.yeyuanqi.hottop.untils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


/**
 * 启动爬虫脚本
 * author  yeyuanqi
 * since   2021.11.15
 */
@Component
@EnableAsync
public class PythonUntils {
    private static final String tengxunpath = "src/main/resources/python/BiSheTengxunxinwenTest.py";
    private static final String baidupath = "src/main/resources/python/bishebaidutest.py";
    private static final String weibopath = "src/main/resources/python/BiSheWeiBoTest.py";
    private static final String jinritoutiaopath = "src/main/resources/python/bishejinritoutiaotest.py";
    private static final String wordcloudpath = "src/main/resources/python/Wordcloud.py";

    private static final Logger logger = LoggerFactory.getLogger(PythonUntils.class);

    @Async
    public void starttengxunxinwen() {
        Process proc;
        try {
            proc = Runtime.getRuntime().exec("python " + tengxunpath);// 执行py文件
            //用输入输出流来截取结果
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"utf8"));
            String line = null;
            while ((line = in.readLine()) != null) {
                logger.info(line);
            }
            in.close();
            proc.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Async
    public void startweibo() {
        Process proc;
        try {
            proc = Runtime.getRuntime().exec("python " + weibopath);// 执行py文件
            //用输入输出流来截取结果
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"utf8"));
            String line = null;
            while ((line = in.readLine()) != null) {
                logger.info(line);
            }
            in.close();
            proc.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Async
    public void startbaidu() {
        Process proc;
        try {
            proc = Runtime.getRuntime().exec("python " + baidupath);// 执行py文件
//            用输入输出流来截取结果
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"utf8"));
            String line = null;
            while ((line = in.readLine()) != null) {
                logger.info(line);
            }
            in.close();
            proc.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Async
    public void startjinritoutiao() {
        Process proc;
        try {
            proc = Runtime.getRuntime().exec("python " + jinritoutiaopath);// 执行py文件
//            用输入输出流来截取结果
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"utf8"));
            String line = null;
            while ((line = in.readLine()) != null) {
                logger.info(line);
            }
            in.close();
            proc.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Async
    public void startWordcloud() {
        Process proc;
        try {
            proc = Runtime.getRuntime().exec("python " + wordcloudpath);// 执行py文件
//            用输入输出流来截取结果
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"utf8"));
            String line = null;
            while ((line = in.readLine()) != null) {
                logger.info(line);
            }
            in.close();
            proc.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
